﻿using System;
using System.Collections.Generic;
using EscapeZone.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XnaPlus.Common.Extensions;

namespace EscapeZone.Weapons
{
    public class Pistol
    {
        private readonly EscapeGame m_game;
        private readonly GameScreen m_screen;
        public static Texture2D ProjectileTexture { get; set; }

        private TimeSpan m_lastFire;

        public float ProjectileSpeed { get; set; }

        public int Ammo { get; set; }

        public float Range { get; set; }

        public TimeSpan Rate { get; set; }

        public Pistol(EscapeGame game, GameScreen screen)
        {
            m_game = game;
            Range = 320;
            Rate = TimeSpan.FromMilliseconds(300);
            ProjectileSpeed = 12;
            m_screen = screen;
            m_lastFire = TimeSpan.Zero;
        }

        public Projectile Fire(Vector2 position, float heading, GameTime gameTime)
        {
            if (Ammo > 0 && gameTime.TotalGameTime > m_lastFire + Rate)
            {
                Ammo--;
                m_lastFire = gameTime.TotalGameTime;

                Projectile p = new Projectile(m_game, m_screen, position)
                                   {
                                       Velocity = Vector2Extension.UnitTheta(heading)*ProjectileSpeed,
                                       Range = Range,
                                   };

                return p;
            }

            return null;
        }
    }
}